﻿using System.Xml;
using System;
using System.IO;
using System.Diagnostics;
using System.Collections;
namespace SudokuPanel
{
    public class SudokuLibrary : IEnumerable
    {
        private XmlDocument xmlDoc = new XmlDocument();
        private XmlNode root = null;
        private String strLibraryFile = "";
        private int _lvl = 0;

        public readonly int library_version = 1;

        public SudokuLibrary(String strLibraryFile, int lvl)
        {
            this.strLibraryFile = strLibraryFile;
            _lvl = lvl;
            Reload();
        }
        public static void removeSudokuPrefix(ref String strSudoku)
        {
            if (strSudoku.StartsWith("KLSUDOKU"))
            {
                strSudoku = strSudoku.TrimStart("KLSUDOKU".ToCharArray());
            }
        }
        public void Save()
        {
            xmlDoc.Save(strLibraryFile);
        }
        public void Reload()
        {
            if (File.Exists(strLibraryFile))
            {
                try
                {
                    xmlDoc.Load(strLibraryFile);
                }
                catch (Exception)
                {
                    xmlDoc = new XmlDocument();
                }
            }
            else
                xmlDoc = new XmlDocument();
            root = xmlDoc.SelectSingleNode("lvl");
            //应该有版本属性            
            if (root != null)
            {
                //判断版本是否是支持的，不支持就清除掉
                String strVersion = ((XmlElement)root).GetAttribute("library_version");
                if (strVersion == "" || pubfun.str2int(strVersion) != library_version)
                {
                    xmlDoc = new XmlDocument();
                    root = null;
                }
            }
            if (root == null)
            {
                XmlElement xe = xmlDoc.CreateElement("lvl");
                xe.SetAttribute("value", _lvl.ToString());
                xe.SetAttribute("library_version", library_version.ToString());
                root = xe;
                xmlDoc.AppendChild(root);
            }

        }
        public XmlNode insertSudoku(String strSudoku, bool save)
        {
            XmlNode xn = xmlDoc.CreateElement("KLSUDOKU" + strSudoku);
            root.AppendChild(xn);
            if (save)
                Save();
            return xn;
        }
        public XmlNode getSudoku(String strSudoku)
        {
//             if (this.Count < 1)
//                 throw new Exception("sudoku library has no sudoku!");
            return root.SelectSingleNode("KLSUDOKU" + strSudoku);
        }

        public XmlNode randGetSudoku()
        {
            //if (this.Count < 1)
            //    throw new Exception("sudoku library has no sudoku!");
            Random ro = new Random();
            int id = ro.Next(0, this.Count - 1);
            return root.ChildNodes[id];
        }

        public bool removeSudoku(String strSudoku)
        {
            try
            {
                XmlNode xn = getSudoku(strSudoku);
                if (xn == null)
                    return false;
                root.RemoveChild(xn);
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return false;
            }
        }
        public bool removeSudoku(XmlNode xn)
        {
            try
            {
                root.RemoveChild(xn);
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                return false;
            }
        }

        public int Count
        {
            get
            {
                return root.ChildNodes.Count;
            }
        }

        public bool remvoeAll()
        {
            if (root != null)
            {
                root.RemoveAll();
                return true;
            }
            return false;
        }

        public IEnumerator GetEnumerator()
        {
            foreach (XmlNode xn in root)
            {
                yield return xn;
            }
        }

    }
}
